package com.jiejie.quartzTask;

import com.jiejie.mapper.CronMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;

@Component
@EnableScheduling
public class OrderByTask implements SchedulingConfigurer {


//    @Scheduled(cron = "*/30 * * * * ?")
    public void configureTasks(){
        System.out.println("静态定时器"+LocalDateTime.now());
    }
    @Autowired
    CronMapper cronMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.addTriggerTask(
                //添加任务
                () -> System.out.println("定时删除任务已开启:"+redisTemplate.delete("OrderBy_Movie::OrderByMovie")),
                triggerContext -> {
                    String cron = cronMapper.getCron();
                    return new CronTrigger(cron).nextExecutionTime(triggerContext);
                }
        );
    }
}
